<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">-->
<title><c:out value="${not empty app.id ? '修改应用信息':'新增应用'}" /></title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<jsp:include page="../common/import.jsp"></jsp:include>
</head>
<style type="text/css">
html {
	height: auto;
}
</style>
<body>
     <!-- <div class="iframe-cot pt0" id="smIframe"> -->
       <div class="right-form mt0">
       <form class="add-form" id="editForm" method="post" action="#" onsubmit="return false;">
          <input id="s_appId" type="hidden" value="${app.id}">
		<ul class="w100 layer-form">
               <li>
                   <label class="ser-label"><em style="color:red;">*</em>应用名称：</label>
                   <div class="form-content">
                       <input id="s_appName" name="appName" class="inputText" value="${app.appName}" type="text">
                       <div class="form-tips text-red" ></div>
                   </div>
               </li>
               <li>
                   <label class="ser-label"><em class="text-red mr5">*</em>业务类型：</label>
                   <div class="form-content">
                      <select id="s_businessType" name="businiessType" class="form-control-select">
                                  <option value="">请选择</option>
									<c:forEach items="${appTypeList}" var="rt">
										<option value="${rt.code }" <c:if test="${rt.code eq app.businessType }"  >selected</c:if> >${rt.name }</option>
									</c:forEach>
                              </select>
                               <div class="form-tips text-red" ></div>
                   </div>
               </li>
               <li>
                   <label class="ser-label"><em class="text-red mr5">*</em>应用url：</label>
                   <div class="form-content">
					  <input id="s_appUrl" name="appUrl" class="inputText" value="${app.appUrl}" type="text">
					   <div class="form-tips text-red" ></div>
                   </div>
               </li>             
               <li >
                   <label class="ser-label"><em class="text-red mr5">*</em>图标样式：</label>
                   <div class="form-content clearfix">
                       <input id="s_iconClass" name="iconClass" class="inputText" value="${app.iconClass}" type="text">
                   	   <div class="form-tips text-red" ></div>
                   </div>
               </li>
               <li >
                   <label class="ser-label"><em class="text-red mr5">*</em>应用编码：</label>
                   <div class="form-content clearfix">
                   		<input id="s_appCode_old" name="appCodeOld" type="hidden" value="${app.appCode }"/>
                         <input id="s_appCode" name="appCode" class="inputText" value="${app.appCode}" type="text" onblur="checkAppCode()">
                   		 <div class="form-tips text-red" id="appCodeTip"></div>
                   </div>
               </li>
               <li >
                   <label class="ser-label"><em class="text-red mr5">*</em>排序号：</label>
                   <div class="form-content clearfix">
						<input id="s_appOrder" name="appOrder" class="inputText" value="${app.appOrder}" type="text">
                   		<div class="form-tips text-red" ></div>
                   </div>
               </li>
               <li >
                   <label class="ser-label"><em class="text-red mr5"></em>描述：</label>
                   <div class="form-content clearfix">
                     <input id="s_description" name="description" class="inputText" value="${app.description}" type="text">
                   <div class="form-tips text-red" ></div>
                   </div>
               </li>	  
                  
               <li class="w-per">
                   <label class="ser-label">&nbsp;</label>
                   <div class="form-content clearfix">
                       <button class="btn btn-orange" id="savebtn" onclick="save()" ><i class="fa fa-save"></i> 保 存 </button>
                       <button id="cancelBtn" class="btn btn-default" onclick="closeLayerOpen()"><i class="fa fa-reply"></i> 返 回 </button>
                   </div>
               </li>
            </ul>
    	</form>
    </div>
 <!--  </div> -->
</body>
<script type="text/javascript">
function closeLayerOpen(){
	parent.layer.close(parent.layer.getFrameIndex(window.name));
}

$("#editForm").validate({
	onkeyup: false,
	onsubmit: false,
	onfocusout:  function(element){
	      $(element).valid() ;
	      
	}, 
	
	rules : {
		appName : {
			required : true,
			maxlength:50,
			isContainsSpecialChar: true 
		},
		businiessType : {
			required : true,
			
		},
		appUrl : {
			required : true,
			maxlength:100,
			iamUrl: true 
		},
		iconClass: {
			required : true,
			maxlength:50,
			specialCharValidateIAM:true
		},
		appCode : {
			required : true,
			maxlength:50,
			isContainsSpecialChar: true
		},
		appOrder : {
			required : true,
			isDigits: true,
			range : [1,10000]
		},
		description:{
			maxlength:50,
		}
	},
	messages : {					
		appName : {
			required : "请完善必填信息",
			maxlength : "请输入正确的应用名称",
			isContainsSpecialChar : "请输入正确的应用名称"
		},
		businiessType : {
			required : "请完善必填信息",
			isContainsSpecialChar : "请选择正确的业务类型"
		},
		appUrl : {
			required : "请完善必填信息",
			isContainsSpecialChar : "请输入正确的应用url"
		},
		
		iconClass: {
			required : "请完善必填信息",
			isContainsSpecialChar: "请输入正确图标样式"
		},
		appCode : {
			required : "请完善必填信息",
			maxlength:"请输入正确的应用编码",
			isContainsSpecialChar: "请输入正确的应用编码"
		},
		appOrder : {
			required : "请完善必填信息",
			isDigits: "请输入正确的排序号",
			range : "请输入正确的排序号"
		},
		description:{
			maxlength:"请输入正确的描述"
		}
	},
	errorPlacement: function(error, element) {
		 if(error){    //error存在的时候
			 $(element).next().html(error); 
			
		 }                              
		 $(element).addClass('input-danger');
		 
	 },
	 success:function(label){
		 $(label).parent().prev().removeClass('input-danger');
		
	 }
	

});

function save(){
	 
	 if(!$("#s_appName").valid() || !$("#s_businessType").valid() || !$("#s_businessType").valid()
			||!$("#s_appUrl").valid() || !$("#s_iconClass").valid() || !$("#s_appCode").valid()
			||!$("#s_appOrder").valid() || !$("#s_description").valid()){
		
		return false;
	} 
	
	var id=$.trim($("#s_appId").val());
 	var appName=$("#s_appName").val();
	var businessType=$("#s_businessType").val();
	var appUrl = $("#s_appUrl").val();
	
	var iconClass = $.trim($("#s_iconClass").val());
	var appCode = $.trim($("#s_appCode").val());
	var appOrder = $.trim($("#s_appOrder").val());
	var description = $.trim($("#s_description").val());
	var data={
			id:$.trim(id),
			appName:$.trim(appName),
			businessType:$.trim(businessType),
			appUrl:$.trim(appUrl),
			iconClass:iconClass,
			appOrder:appOrder,
			appCode:appCode,
			description:description
			}; 
	if(!checkAppCode()){
		
		return;
	}
	
	//防止多次点击
	$("#savebtn").attr('disabled','disabled');
	
 	$.post("${ctx}/application/save",data,function(result){
		if(result.code=="success"){
			 successTip();
			 if(id == null || id == ''){
				 window.parent.jumpPage(1);
			 }else{
				 window.parent.jumpPage();
			 }
			 
			 timeCloseFrame();
		}else{
			errorTip();
			$("#savebtn").removeAttr('disabled');
		}
	},"json");  
}

function checkAppCode(){
	if(!$("#s_appCode").valid()){
		return false;
	}
	var isAppCodeFit = false;
	var appCode = $.trim($("#s_appCode").val());
	var oldAppCode = $.trim($("#s_appCode_old").val());
	var appId = $("#s_appId").val();
	//修改页面
	/* console.log("oldcode= "+ oldAppCode +"code= " + appCode); */
	if(appId != null || appId != ''){
		if(appCode == oldAppCode){
			isAppCodeFit = true;
			$("#appCodeTip").html("");
			return true;
		}
	}
	$.ajax({
		url:'${ctx}/application/check/appcode',
		async:false,//用同步
		type:"post",
		data:{'appCode':appCode},
		dataType:"json",
		success:function(data){
			if(data.code == 'notexist'){
				isAppCodeFit = true;
				$("#appCodeTip").html("");
			}else if(data.code=='exist'){
				isAppCodeFit = false;
				$("#appCodeTip").html("<span style='color:red;'>该应用编码已存在</span>");	
			}else{
				isAppCodeFit = false;
				$("#appCodeTip").html("<span style='color:red;'>系统出错，校验失败</span>");
			}
		},
		error:function(){
			isAppCodeFit = false;
		}
	});
	
	return isAppCodeFit;
}

</script>

</html>
